package com.example.actionmode;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private final String[] names= new String[]{"One","Two","Three","Four","Five"};
    private ListView listView;
    private List<Item> list;
    private BaseAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //绑定xml布局文件里面的listview控件
        listView = findViewById(R.id.mylist);
        list = new ArrayList<Item>();


        //把要显示的数据存储到列表
        for(int i = 0;i < names.length;i++){
            //添加一行
            list.add(new Item(names[i],false));
        }

        //对listview进行适配器装配
        adapter = new Adapter(list,MainActivity.this);
        listView.setAdapter(adapter);

        //定义多选
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
        //定义选中数目
            int num = 0;


            /*
            * ActionMode是长按后的标题栏
            * position是选中Item的序号
            * id是选中item的id
            * checked如果item选中就为true，没选中就为false
            */
            @Override
            public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
                if(checked == true){
                    list.get(position).setSelected(true);
                    //刷新
                    adapter.notifyDataSetChanged();
                    num++;
                }
                else{
                    list.get(position).setSelected(false);
                    //刷新
                    num--;
                }
                //用TextView显示
                mode.setTitle(" "+ num + " Selected");


            }

            /*
            * ActionMode是长按后的标题栏
            * Menu是标题栏显示的菜单内容
             */
            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                //设置长按后显示的标题栏的内容
                MenuInflater inflater = mode.getMenuInflater();
                inflater.inflate(R.menu.menu,menu);
                num=0;
                adapter.notifyDataSetChanged();
                return true;
            }
            /*
            * 标题栏UI创建和更新
             */
            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                adapter.notifyDataSetChanged();
                return false;
            }
            public void refresh(){
                for(int i = 0;i < names.length;i++){
                    list.get(i).setSelected(false);
                }

            }

            /*
            * 此方法可以监听标题栏，进行相应操作
            * 设置actionmode菜单每个按钮的点击事件
             */


            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                switch (item.getItemId()){
                    //删除
                    case R.id.menu_delete:
                        adapter.notifyDataSetChanged();
                        num = 0;
                        refresh();
                        mode.finish();
                        return true;
                    default:
                        refresh();
                        adapter.notifyDataSetChanged();
                        num = 0;
                        return false;
                }
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {
                    refresh();
                    adapter.notifyDataSetChanged();
            }
        });
    }









}